var express = require('express');
var session = require('express-session'); /*引入会话变量*/
const path = require('path');
const { createProxyMiddleware } = require('http-proxy-middleware');
var cp = require('child_process');

const devServer = {
    host: '127.0.0.1',
    port: '8080',
    proxy: {
        // "/": {
        //     target: 'http://127.0.0.1:8080/',
        //     // pathRewrite: { '^/lotteryxgm': '/' },
        //     changeOrigin: true
        // },
        // "/api": {
        //     target: 'http://127.0.0.1/lottery/',
        //     pathRewrite: { '^/api': '/' },
        //     changeOrigin: true
        // },
        // "/api": {
        //     target: 'http://192.168.32.250:32580/lottery/',
        //     pathRewrite: { '^/api': '/' },
        //     changeOrigin: true
        // },
        // "/sso": {
        //     target: 'http://192.168.32.250:30067/',
        //     pathRewrite: { '^/sso': '/' },
        //     changeOrigin: true
        // },
        // "/commodity": {
        //     target: 'http://192.168.32.250:30095/',
        //     pathRewrite: { '^/commodity': '/' },
        //     changeOrigin: true
        // },
        // "/user": {
        //     target: 'http://192.168.32.250:30092/',
        //     pathRewrite: { '^/user': '/' },
        //     changeOrigin: true
        // },
        // "/WeChatShare": {
        //     target: 'https://wx.telefen.com/WeChatShare/',
        //     pathRewrite: { '^/WeChatShare': '/' },
        //     changeOrigin: true
        // }
    }
}


var app = express();
// app.use(express.bodyParser());
//app.use(express.cookieParser());
app.use(session({
    secret: 'recommand 128 bytes random string', // 建议使用 128 个字符的随机字符串
    cookie: { maxAge: 20 * 60 * 1000 }, //cookie生存周期20*60秒
    resave: true, //cookie之间的请求规则,假设每次登陆，就算会话存在也重新保存一次
    saveUninitialized: true //强制保存未初始化的会话到存储器
})); //这些是写在app.js里面的

for (var key in devServer.proxy) {

    var val = {
        target: devServer.proxy[key].target,
        pathRewrite: devServer.proxy[key].pathRewrite,
        changeOrigin: devServer.proxy[key].changeOrigin,
        onProxyRes: function(proxyRes, req, res) {
            console.log('来自：', getIp(req), '的转发请求：', req.method, req.url, req.hostname)
                //res.header("Access-Control-Allow-Origin", "*");
                //res.header("Access-Control-Allow-Origin:*");//允许所有来源访问
                //res.header("Access-Control-Allow-Method:POST,GET");//允许访问的方
        },
    };
    app.use(key, createProxyMiddleware(val));
}

app.use(express.static(path.join(__dirname, './')));
app.listen(devServer.port, function() {
    console.log(`app is running at port:${devServer.port}`);
    console.log(`url: http://${devServer.host}:${devServer.port}`);
    cp.exec(`explorer http://${devServer.host}:${devServer.port}`, function() {});
});


// 获取来自请求主机的ip
var getIp = function(req) {
    var ip = req.headers['x-real-ip'] ||
        req.headers['x-forwarded-for'] ||
        req.socket.remoteAddress || '';
    if (ip.split(',').length > 0) {
        ip = ip.split(',')[0];
    }
    return ip;
}